package info.erge.sort;

public class SortingHelper {
    
    private SortingHelper() {}

    public static <E extends Comparable<E>> boolean isSorted(E[] arr) {
        for (int i = 1; i < arr.length; i++) {
            if (arr[i - 1].compareTo(arr[i]) > 0) {
                return false;
            }
        }
        return true;
    }

    public static  <E extends Comparable<E>> void sortTest(String sortname, E[] arr) {
        long start = System.nanoTime();
        if (sortname.equals("SelectionSort")) {
            SelectionSort.sort(arr);
        }
        if (sortname.equals("InsertionSort")) {
            InsertionSort.sort(arr);
        }
        long end = System.nanoTime();
        if (!SortingHelper.isSorted(arr)) {
            throw new RuntimeException(sortname + " fail");
        }
        System.out.println(String.format("%s, n = %d, %f s", sortname, arr.length, (end - start) / 1000000000.0));
    }
}
